package com.lovo.bbs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;

import com.lovo.bbs.po.LinkPo;
import com.lovo.bbs.util.DbUtil;
import com.lovo.bbs.util.MysqlDb;

/**
 * 友情链接Dao
 * 
 * @author tiancen2001
 * 
 */
public class LinkDao {
	// 操作类型
	public static final int OPER_OF_INSERT = 1;
	public static final int OPER_OF_UPDATE = 2;
	public static final int OPER_OF_DELETE = 3;

	public LinkDao(){
		
	}
	/**
	 * 返回所有友情链接Po列表
	 * 
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	public  ArrayList<LinkPo> getAllLink() throws NamingException,
			SQLException {
		ArrayList<LinkPo> pos = new ArrayList<LinkPo>();

		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);

		String sql = "select linkid, linkname, aboutlink, url from link";

		ResultSet rs = con.prepareStatement(sql).executeQuery();
		while (rs.next()) {
			LinkPo po = new LinkPo();
			po.setLinkid(rs.getInt(1));
			po.setLinkname(rs.getString(2));
			po.setAboutLink(rs.getString(3));
			po.setLinkurl(rs.getString(4));
			pos.add(po);
		}
		mysqldb.closeRS(rs);
		mysqldb.closeConnection(con);
		return pos;
	}

	/**
	 * 返回指定链接
	 * 
	 * @return
	 * @throws SQLException
	 * @throws NamingException
	 */
	public  LinkPo getLinkByID(int linkid) throws NamingException,
			SQLException {
		LinkPo po = new LinkPo();

		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);

		String sql = "select linkid, linkname, aboutlink, url from link  where linkid="
				+ linkid;
		ResultSet rs = con.prepareStatement(sql).executeQuery();
		if (rs.next()) {
			po.setLinkid(rs.getInt(1));
			po.setLinkname(rs.getString(2));
			po.setAboutLink(rs.getString(3));
			po.setLinkurl(rs.getString(4));
		}
		mysqldb.closeRS(rs);
		mysqldb.closeConnection(con);
		return po;
	}

	/**
	 * 链接控制台更新
	 * 
	 * @throws SQLException
	 * @throws NamingException
	 */
	public  int updateLink(LinkPo po, int operType)
			throws NamingException, SQLException {
		int updated = 0;

		MysqlDb mysqldb = new MysqlDb();
		mysqldb.setPool(true);
		Connection con = DbUtil.getConn(mysqldb);
		con.setAutoCommit(false);

		if (operType == LinkDao.OPER_OF_INSERT) {// 插入
			String sqlInsert = "insert into link(linkname, aboutlink, url) values(?,?,?)";
			PreparedStatement ps = con.prepareStatement(sqlInsert);
			ps.setString(1, po.getLinkname());
			ps.setString(2, po.getAboutLink());
			ps.setString(3, po.getLinkurl());
			updated = ps.executeUpdate();
			mysqldb.closePS(ps);
		} else if (operType == LinkDao.OPER_OF_UPDATE) {// 更新
			String sqlUpdate = "update link set  linkname=?,aboutlink=?,url=?  where linkid=?";
			PreparedStatement ps = con.prepareStatement(sqlUpdate);
			ps.setString(1, po.getLinkname());
			ps.setString(2, po.getAboutLink());
			ps.setString(3, po.getLinkurl());
			ps.setInt(4, po.getLinkid());
			updated = ps.executeUpdate();
			mysqldb.closePS(ps);
		} else if (operType == LinkDao.OPER_OF_DELETE) {// 删除
			String sqlDelete = "delete from link where linkid=?";
			PreparedStatement ps = con.prepareStatement(sqlDelete);
			ps.setInt(1, po.getLinkid());
			updated = ps.executeUpdate();
			mysqldb.closePS(ps);
		}

		con.commit();
		mysqldb.closeConnection(con);
		return updated;
	}

}
